package com.shiku.imserver.message.hander;

import com.shiku.imserver.common.annotation.MessageCommandAnnotation;
import com.shiku.imserver.common.constant.KConstants;
import com.shiku.imserver.common.hander.AbstractMessageHandler;
import com.shiku.imserver.common.message.ChatMessage;
import com.shiku.imserver.common.message.ExitGroupMessage;
import com.shiku.imserver.common.message.GroupMessageResp;
import com.shiku.imserver.common.message.JoinGroupMessage;
import com.shiku.imserver.common.message.PacketVO;
import com.shiku.imserver.common.packets.ImPacket;
import com.shiku.imserver.common.proto.MessageProBuf;
import com.shiku.imserver.common.utils.ProBufUtils;
import com.shiku.imserver.common.utils.StringUtils;
import com.shiku.imserver.message.MessageFactory;
import com.shiku.imserver.service.IMBeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;

public class GroupMessageHandler extends AbstractMessageHandler {
    private static final Logger logger = LoggerFactory.getLogger(GroupMessageHandler.class);

    @MessageCommandAnnotation(command = 20)
    public PacketVO joinGroupHandler(ImPacket packet, ChannelContext channelContext) {
        try {
            JoinGroupMessage message = (JoinGroupMessage) ProBufUtils.decoderMessageBody(packet.getBytes(), MessageProBuf.JoinGroupMessageProBuf.getDescriptor(), JoinGroupMessage.class);
            String jid = message.getJid();
            long seconds = message.getSeconds();
            String from = message.getMessageHead().getFrom();
            if (KConstants.isDebug) {
                logger.info(" join group userId > {}  jid {}  seconds {}", new Object[]{from, jid, Long.valueOf(seconds)});
            }
            GroupMessageResp resultMessage = MessageFactory.createGroupRequestResult(message, jid, false);
            try {
                if (!StringUtils.isEmpty(jid)) {
                    Tio.bindGroup(channelContext, jid);
                }
                resultMessage.setStatus((byte) 1);
            } catch (Exception e) {
                e.printStackTrace();
                resultMessage.setStatus((byte) -1);
            }
            ImPacket result = ProBufUtils.encodeImPacket(resultMessage, MessageProBuf.GroupMessageRespProBuf.getDescriptor());
            result.setCommand((short)22);
            Tio.send(channelContext, result);
            if (0 >= seconds) {
                return null;
            }
            for (ChatMessage chatMessage : IMBeanUtils.getMessageRepository().loadGroupHistoryMessage(jid, seconds)) {
                chatMessage.getMessageHead().setOffline(true);
                ImPacket imPacket = MessageFactory.convertToImPacket(chatMessage);
                if (imPacket != null) {
                    Tio.send(channelContext, imPacket);
                }
            }
            return null;
        } catch (Exception e2) {
            e2.printStackTrace();
            return null;
        }
    }

    @MessageCommandAnnotation(command = 21)
    public PacketVO exitGroupHandler(ImPacket packet, ChannelContext channelContext) throws Exception {
        try {
            ExitGroupMessage message = (ExitGroupMessage) ProBufUtils.decoderMessageBody(packet.getBytes(), MessageProBuf.ExitGroupMessageProBuf.getDescriptor(), ExitGroupMessage.class);
            String jid = message.getJid();
            String from = message.getMessageHead().getFrom();
            if (KConstants.isDebug) {
                logger.info(" join group userId > {}  jid {}", from, jid);
            }
            GroupMessageResp resultMessage = MessageFactory.createGroupRequestResult(message, jid, true);
            try {
                if (!StringUtils.isEmpty(jid)) {
                    Tio.unbindGroup(jid, channelContext);
                }
                resultMessage.setStatus((byte) 1);
            } catch (Exception e) {
                e.printStackTrace();
                resultMessage.setStatus((byte) -1);
            }
            PacketVO result = ProBufUtils.encodePacketVO(resultMessage, MessageProBuf.GroupMessageRespProBuf.getDescriptor());
            result.setCmd((short)22);
            return result;
        } catch (Exception e2) {
            e2.printStackTrace();
            return null;
        }
    }
}
